// VBConversions Note: VB project level imports
using System.Collections.Generic;
using System;
using System.Linq;
using System.Drawing;
using System.Diagnostics;
using Microsoft.VisualBasic;
using System.Xml.Linq;
using Microsoft.VisualBasic.Compatibility;
using System.Windows.Forms;
using System.Collections;
using System.Data;
// End of VB project level imports

using System.Runtime.InteropServices;
using FastColoredTextBoxNS;
using System.Text.RegularExpressions;


namespace 喷码数据生成工具
{
	public partial class SystemSet
	{
		
#region Default Instance
		
		private static SystemSet defaultInstance;
		
		/// <summary>
		/// Added by the VB.Net to C# Converter to support default instance behavour in C#
		/// </summary>
public static SystemSet Default
		{
			get
			{
				if (defaultInstance == null)
				{
					defaultInstance = new SystemSet();
					defaultInstance.FormClosed += new FormClosedEventHandler(defaultInstance_FormClosed);
				}
				
				return defaultInstance;
			}
			set
			{
				defaultInstance = value;
			}
		}
		
		static void defaultInstance_FormClosed(object sender, FormClosedEventArgs e)
		{
			defaultInstance = null;
		}
		
#endregion
		//指定一个窗口的新父（在vb里使用：利用这个函数，vb可以多种形式支持子窗口。例如，可将控件从一个容器移至窗体中的另一个。用这个函数在窗体间移动控件是相当冒险的，但却不失为一个有效的办法。如真的这样做，请在关闭任何一个窗体之前，注意用SetParent将控件的父设回原来的那个）
		[DllImport("user32", EntryPoint = "SetParent", SetLastError = false, 
		CharSet = CharSet.Auto, ExactSpelling = false, 
		CallingConvention = CallingConvention.StdCall)]public static  extern int SetParent(int hWndChild, 
		int hWndNewParent);
		MSScriptControl.ScriptControl RunVBScript = new MSScriptControl.ScriptControl();
		ModifyAddress DataBaseSet = new ModifyAddress(false);
		private const string DescriptionSql1 = "SELECT  * FROM  ::fn_listextendedproperty (null, \'user\', \'dbo\', \'table\',\'";
		private const string DescriptionSql2 = "\', \'column\', default)";
		int SetPagesIndex = -1;
		//==============================设置代码编辑文本框样式
		TextStyle BlueStyle; // VBConversions Note: Initial value cannot be assigned here since it is non-static.  Assignment has been moved to the class constructors.
		TextStyle BoldStyle = new TextStyle(null, null, (System.Drawing.FontStyle) (FontStyle.Bold | FontStyle.Underline));
		TextStyle GrayStyle; // VBConversions Note: Initial value cannot be assigned here since it is non-static.  Assignment has been moved to the class constructors.
		TextStyle MagentaStyle; // VBConversions Note: Initial value cannot be assigned here since it is non-static.  Assignment has been moved to the class constructors.
		TextStyle GreenStyle; // VBConversions Note: Initial value cannot be assigned here since it is non-static.  Assignment has been moved to the class constructors.
		TextStyle BrownStyle; // VBConversions Note: Initial value cannot be assigned here since it is non-static.  Assignment has been moved to the class constructors.
		TextStyle MaroonStyle; // VBConversions Note: Initial value cannot be assigned here since it is non-static.  Assignment has been moved to the class constructors.
		MarkerStyle SameWordsStyle; // VBConversions Note: Initial value cannot be assigned here since it is non-static.  Assignment has been moved to the class constructors.
		string lang = "VB";
#region 构造函数
		public SystemSet()
		{
			// VBConversions Note: Non-static class variable initialization is below.  Class variables cannot be initially assigned non-static values in C#.
			BlueStyle = new TextStyle(Brushes.Blue, null, FontStyle.Regular);
			GrayStyle = new TextStyle(Brushes.Gray, null, FontStyle.Regular);
			MagentaStyle = new TextStyle(Brushes.Magenta, null, FontStyle.Regular);
			GreenStyle = new TextStyle(Brushes.Green, null, FontStyle.Italic);
			BrownStyle = new TextStyle(Brushes.Brown, null, FontStyle.Italic);
			MaroonStyle = new TextStyle(Brushes.Maroon, null, FontStyle.Regular);
			SameWordsStyle = new MarkerStyle(new SolidBrush(Color.FromArgb(40, Color.Gray)));
			
			
			// 此调用是 Windows 窗体设计器所必需的。
			InitializeComponent();
			
			//Added to support default instance behavour in C#
			if (defaultInstance == null)
				defaultInstance = this;
			
			// 在 InitializeComponent() 调用之后添加任何初始化。
			InitStylesPriority();
		}
		public SystemSet(int SetIndex)
		{
			// VBConversions Note: Non-static class variable initialization is below.  Class variables cannot be initially assigned non-static values in C#.
			BlueStyle = new TextStyle(Brushes.Blue, null, FontStyle.Regular);
			GrayStyle = new TextStyle(Brushes.Gray, null, FontStyle.Regular);
			MagentaStyle = new TextStyle(Brushes.Magenta, null, FontStyle.Regular);
			GreenStyle = new TextStyle(Brushes.Green, null, FontStyle.Italic);
			BrownStyle = new TextStyle(Brushes.Brown, null, FontStyle.Italic);
			MaroonStyle = new TextStyle(Brushes.Maroon, null, FontStyle.Regular);
			SameWordsStyle = new MarkerStyle(new SolidBrush(Color.FromArgb(40, Color.Gray)));
			
			
			// 此调用是 Windows 窗体设计器所必需的。
			InitializeComponent();
			
			//Added to support default instance behavour in C#
			if (defaultInstance == null)
				defaultInstance = this;
			
			// 在 InitializeComponent() 调用之后添加任何初始化。
			SetPagesIndex = SetIndex;
		}
#endregion
		public void SystemSet_Load(object sender, System.EventArgs e)
		{
			SetParent(DataBaseSet.Handle.ToInt32(), System.Convert.ToInt32(SetPages.TabPages[0].Handle.ToInt32()));
			DataBaseSet.Show();
			DataBaseSet.BackColor = Color.White;
			DataBaseSet.FormBorderStyle = Windows.Forms.FormBorderStyle.None;
			DataBaseSet.WindowState = (System.Windows.Forms.FormWindowState) 2;
			//=================加载已保存的数据格式
			LB_CodeList.Items.Clear();
			SaveDataFormat.Items.Clear();
			LB_SearchTable.Items.Clear();
			if (CommFunction.SaveData.Setting != null)
			{
				for (var i = 0; i <= CommFunction.SaveData.Setting.Count - 1; i++)
				{
					SaveDataFormat.Items.Add(CommFunction.SaveData.Setting[i].SaveName);
				}
			}
			if (CommFunction.CheckData != null)
			{
				for (var i = 0; i <= CommFunction.CheckData.Count - 1; i++)
				{
					LB_CodeList.Items.Add(CommFunction.CheckData[System.Convert.ToInt32(i)].CheckName);
				}
			}
			if (CommFunction.SaveData.SearchTable != null)
			{
				for (var i = 0; i <= CommFunction.SaveData.SearchTable.Count - 1; i++)
				{
					LB_SearchTable.Items.Add(CommFunction.SaveData.SearchTable[System.Convert.ToInt32(i)].TableName);
				}
			}
			Btn_AddCode.Enabled = false;
			Btn_SaveCode.Enabled = false;
			RunVBScript.Language = "VBScript";
			if (SetPagesIndex >= 0 & SetPagesIndex < SetPages.TabPages.Count)
			{
				SetPages.SelectedIndex = SetPagesIndex;
			}
			ToolTip1.SetToolTip(TB_LoadDataVBS, "  1.必须是完整的函数过程；" + "\r\n" + "  2.函数有一个参数，参数类型为ArrayList；" + "\r\n" + "  3.函数返回值为字符型数据，并且以 | 符号分隔对应到数据表的每列数据；" + "\r\n" + "  4.返回值中数据段必须等于7。");
			ToolTip1.ToolTipTitle = "编码规则";
			ToolTip1.UseAnimation = true;
			ToolTip1.ShowAlways = true;
			ToolTip1.AutoPopDelay = 3000;
			//  ToolTip1.AutomaticDelay = 1
			for (var i = 0; i <= SetPages.TabPages.Count - 1; i++)
			{
				CommFunction.ChangeButtonFromFrm(SetPages.TabPages[i]);
			}
			CBCodeMode.Items.Add(EnumCodeMode.None.ToString());
			CBCodeMode.Items.Add(CommFunction.EnumCodeMode.VBScript.ToString());
			CBCodeMode.Items.Add(CommFunction.EnumCodeMode.JavaScript.ToString());
			CBCodeMode.SelectedIndex = 0;
			TabPage2_Resize(null, e);
		}
		
		public void Btn_Remove_Click(System.Object sender, System.EventArgs e)
		{
			if (SaveDataFormat.SelectedIndex >= 0 & SaveDataFormat.SelectedIndex < SaveDataFormat.Items.Count)
			{
				string RemoveString = System.Convert.ToString(SaveDataFormat.Items[SaveDataFormat.SelectedIndex]);
				SaveDataFormat.Items.Remove(RemoveString);
				RemovedFormat.Items.Add(RemoveString);
			}
		}
		
		public void Btn_Recoevry_Click(System.Object sender, System.EventArgs e)
		{
			if (RemovedFormat.SelectedIndex >= 0 & RemovedFormat.SelectedIndex < RemovedFormat.Items.Count)
			{
				string Tmp = System.Convert.ToString(RemovedFormat.Items[RemovedFormat.SelectedIndex]);
				RemovedFormat.Items.Remove(Tmp);
				SaveDataFormat.Items.Add(Tmp);
			}
		}
		
		public void Btn_FormatSave_Click(System.Object sender, System.EventArgs e)
		{
			if (RemovedFormat.Items.Count > 0)
			{
				if (CommFunction.SaveData.Setting != null)
				{
					int SC = CommFunction.SaveData.Setting.Count;
					int RC = RemovedFormat.Items.Count;
					string tmp = "";
					int RemoveCount = 0;
					for (var i = 0; i <= RC - 1; i++)
					{
						tmp = RemovedFormat.Items[System.Convert.ToInt32(i)].ToString();
						for (var j = 0; j <= SC - 1; j++)
						{
							if (tmp.ToUpper() == CommFunction.SaveData.Setting[System.Convert.ToInt32(j)].SaveName.ToUpper)
							{
								CommFunction.SaveData.Setting.RemoveAt(System.Convert.ToInt32(j));
								Form1.Default.RemoveSetting(tmp);
								RemoveCount++;
								break;
							}
						}
					}
					if (Interaction.MsgBox("是否删除当前选中的数据格式多个数据！", (int) MsgBoxStyle.Question + MsgBoxStyle.YesNo, "询问") == MsgBoxResult.Yes)
					{
						Form1.Default.SaveSetting();
						RemovedFormat.Items.Clear();
						Interaction.MsgBox("数据保存成功，共删除：" + System.Convert.ToString(RemoveCount) + "条记录！", (int) MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示");
					}
				}
				
			}
		}
		
		public void Btn_RunCode_Click(System.Object sender, System.EventArgs e)
		{
			string Result = "";
			string gCode = "";
			try
			{
				RunVBScript.Language = CBCodeMode.Text;
				gCode = CheckCode_Script.Text;
				RunVBScript.AddCode(gCode);
				string temp_Parameters = TB_TestData.Text;
				Result = System.Convert.ToString(RunVBScript.Run(TB_CheckFunName.Text, ref temp_Parameters));
				TB_TestData.Text = temp_Parameters;
				if (string.IsNullOrEmpty(Result))
				{
					MessageBox.Show(this, "脚本函数未返回值！", "脚本错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				return ;
//				CheckLength.Text = System.Convert.ToString(Result.Length);
//				Interaction.MsgBox("测试结果为：" + Result, MsgBoxStyle.Information, "提示");
//				Btn_AddCode.Enabled = true;
//				Btn_SaveCode.Enabled = true;
			}
			catch (Exception ex)
			{
				Btn_AddCode.Enabled = false;
				Btn_SaveCode.Enabled = false;
				Interaction.MsgBox(ex.Message, (int) MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "错误");
			}
		}
		
		public void Btn_AddCode_Click(System.Object sender, System.EventArgs e)
		{
			if (string.IsNullOrEmpty(TB_CheckName.Text) || string.IsNullOrEmpty(TB_CheckFunName.Text))
			{
				Interaction.MsgBox("校验方式名称和函数名称，不能为空！", (int) MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "提示");
			}
			return ;
//			if (Information.IsNumeric(CheckLength.Text) == false)
//			{
//				Interaction.MsgBox("校验码长度必须是数值型数据！", MsgBoxStyle.Critical, "提示");
//				}
//				return ;
//				if (Conversion.Val(CheckLength.Text) <= 0)
//				{
//					Interaction.MsgBox("校验码长度必须大于或等于1！", MsgBoxStyle.Critical, "提示");
//					}
//					return ;
//					CommFunction.CheckMode tmpCheckMode = new CommFunction.CheckMode();
//					tmpCheckMode.CheckName = TB_CheckName.Text;
//					tmpCheckMode.CheckFunName = TB_CheckFunName.Text;
//					tmpCheckMode.CheckLength = (int) CheckLength.Text;
//					tmpCheckMode.CheckDescription = CheckDescription.Text;
//					if (CBCodeMode.Text.ToLower() == "javascript")
//					{
//						tmpCheckMode.CodeMode = CommFunction.EnumCodeMode.JavaScript;
//						}
//						else if (CBCodeMode.Text.ToLower() == "vbscript")
//						{
//							tmpCheckMode.CodeMode = CommFunction.EnumCodeMode.VBScript;
//							}
//							else
//							{
//								MessageBox.Show(this, "校验码脚本类型必须为VBScript或者JavaScript!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
//								return ;
//								}
//								if (CommFunction.CheckData != null)
//								{
//									for (var i = 0; i <= CommFunction.CheckData.Count - 1; i++)
//									{
//										if (tmpCheckMode.CheckName.ToUpper() == CommFunction.CheckData[System.Convert.ToInt32(i)].CheckName.ToUpper)
//										{
//											Interaction.MsgBox("校验方式已经存在，请确定是否已经添加过此校验方式！", MsgBoxStyle.Critical, "错误");
//											}
//											return ;
//											if (tmpCheckMode.CheckFunName.ToUpper() == CommFunction.CheckData[System.Convert.ToInt32(i)].CheckFunName.ToUpper)
//											{
//												Interaction.MsgBox("校验方式函数名重复，请修改校验方式函数名！", MsgBoxStyle.Critical, "错误");
//												}
//												return ;
//												}
//												}
												//  tmpCheckMode.CheckCode = getCheckFunction()
//												tmpCheckMode.CheckCode = CheckCode_Script.Text;
//												if (CommFunction.SaveData.CheckNumberMode != null)
//												{
//													CommFunction.SaveData.CheckNumberMode.Add(tmpCheckMode);
//													}
//													else
//													{
//														CommFunction.SaveData.CheckNumberMode = new List<CommFunction.CheckMode>();
//														CommFunction.SaveData.CheckNumberMode.Add(tmpCheckMode);
//														}
//														LB_CodeList.Items.Add(tmpCheckMode.CheckName);
//														Form1.Default.SaveSetting();
//														Form1.Default.ReLoadCheckMode();
//														Interaction.MsgBox("【" + tmpCheckMode.CheckName + "】校验方式已经保存，请先测试准确后再使用！", MsgBoxStyle.Information, "提示");
													}
													private string getCheckFunction()
													{
														string FunName = TB_CheckFunName.Text;
														string FunCode = CheckCode_Script.Text;
														string[] LinesCode = null;
														string tmpName = "";
														bool NameExist = false;
														bool EndCode = false;
														string Param = "";
														int StartLine = 0;
														int EndLine = 0;
														string Result = "";
														int CodeStartLine = -1;
														string TmpCode = "";
														LinesCode = FunCode.Split("\r\n");
														for (var i = 0; i <= LinesCode.Length - 1; i++)
														{
															TmpCode = LinesCode[(int) i].Trim();
															if (string.IsNullOrEmpty(TmpCode) == false && CodeStartLine < 0)
															{
																CodeStartLine = System.Convert.ToInt32(i);
															}
															if (TmpCode.ToUpper().IndexOf("function ".ToUpper()) + 1 > 0)
															{
																int a = TmpCode.IndexOf("(");
																int c = TmpCode.IndexOf(")");
																int b = TmpCode.ToUpper().IndexOf("function ".ToUpper());
																if (a > 8)
																{
																	tmpName = TmpCode.Substring(b + 9, a - b - 9).Trim();
																	if (b - a + 1 > 0)
																	{
																		Param = TmpCode.Substring(a + 1, b - a + 1);
																	}
																	StartLine = System.Convert.ToInt32(i);
																	NameExist = true;
																}
															}
															if (TmpCode.ToUpper().IndexOf("end ".ToUpper()) + 1 > 0 && (TmpCode.ToUpper().IndexOf(" function".ToUpper()) + 1 ) > 0)
															{
																if (TmpCode.Replace(" ", "").ToUpper().Trim() == "endfunction".ToUpper())
																{
																	EndCode = true;
																	EndLine = System.Convert.ToInt32(i);
																	break;
																}
															}
														}
														// MsgBox(CodeStartLine & "|" & StartLine)
														// If NameExist And EndCode Then If Param = "" Or Param.IndexOf(",") >= 0 Then MsgBox("参数和校验需求不匹配！" & Param, MsgBoxStyle.Critical, "错误") : Return ""
														//If NameExist = False Or EndCode = False Then MsgBox("函数过程不完整！", MsgBoxStyle.Critical, "编码错误") : Return ""
														if (NameExist && EndCode)
														{
															if (FunName.ToUpper().Trim() != tmpName.ToUpper().Trim() && StartLine == CodeStartLine)
															{
																throw (new Exception("函数名与设定不匹配，请重新输入函数名！" + FunName + "|" + tmpName));
															}
															return "";
//															if (FunName.ToUpper().Trim() == tmpName.ToUpper().Trim() && string.IsNullOrEmpty(FunName) == true)
//															{
//																throw (new Exception("未设置函数名，请修改源码和设置！"));
//																}
//																return "";
															}
															if (CodeStartLine != StartLine & StartLine > CodeStartLine)
															{
																Result = "function " + FunName + "(a)";
																for (var i = 0; i <= StartLine - 1; i++)
																{
																	Result = Result + "\r\n" + LinesCode[(int) i];
																}
																Result = Result + "\r\n" + "end function";
																for (var i = StartLine; i <= LinesCode.Length - 1; i++)
																{
																	Result = Result + "\r\n" + LinesCode[(int) i];
																}
																return Result;
															}
															else
															{
																if (EndLine > StartLine)
																{
																	for (var i = 0; i <= LinesCode.Length - 1; i++)
																	{
																		if (Result != "")
																		{
																			Result = Result + "\r\n" + LinesCode[(int) i];
																		}
																		else
																		{
																			Result = LinesCode[(int) i];
																		}
																	}
																	return Result;
																}
																else
																{
																	return "function " + FunName + "(a)" + "\r\n" + FunCode + "\r\n" + "end function";
																}
															}
														}
														
														public void Btn_SaveCode_Click(System.Object sender, System.EventArgs e)
														{
															string tmp = TB_CheckName.Text;
															string sc = TB_CheckFunName.Text;
															int ObjIndex = -1;
															CommFunction.CheckMode ModyfyCheck = new CommFunction.CheckMode();
															if (CommFunction.SaveData.CheckNumberMode != null)
															{
																for (var i = 0; i <= CommFunction.SaveData.CheckNumberMode.Count - 1; i++)
																{
																	if (CommFunction.SaveData.CheckNumberMode[System.Convert.ToInt32(i)].CheckName.ToUpper == tmp.ToUpper() && CommFunction.SaveData.CheckNumberMode[System.Convert.ToInt32(i)].CheckFunName.ToUpper == sc.ToUpper())
																	{
																		ObjIndex = System.Convert.ToInt32(i);
																		ModyfyCheck = CommFunction.SaveData.CheckNumberMode[System.Convert.ToInt32(i)];
																	}
																}
															}
															if (ObjIndex >= 0)
															{
																ModyfyCheck.CheckName = TB_CheckName.Text;
																ModyfyCheck.CheckLength = (int) CheckLength.Text;
																ModyfyCheck.CheckFunName = TB_CheckFunName.Text;
																ModyfyCheck.CheckDescription = CheckDescription.Text;
																ModyfyCheck.CheckCode = CheckCode_Script.Text;
																if (ModyfyCheck.CodeMode.ToString() != CBCodeMode.Text)
																{
																	MessageBox.Show(this, "不能修改脚本代码类型，如需修改代码类型请重新添加!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
																}
																return ;
//																if (CBCodeMode.Text.ToLower() == "JavaScript".ToLower())
//																{
//																	ModyfyCheck.CodeMode = CommFunction.EnumCodeMode.JavaScript;
//																	}
//																	else if (CBCodeMode.Text.ToLower() == "VBScript".ToLower())
//																	{
//																		ModyfyCheck.CodeMode = CommFunction.EnumCodeMode.VBScript;
//																		}
//																		else
//																		{
//																			MessageBox.Show(this, "校验码脚本类型必须为VBScript或者JavaScript!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
//																			return ;
//																			}
																			CommFunction.SaveData.CheckNumberMode[ObjIndex] = ModyfyCheck;
																			Form1.Default.SaveSetting();
																			Interaction.MsgBox("【" + ModyfyCheck.CheckName + "]校验方式修改成功，请注意测试校验方式的准确性！", MsgBoxStyle.Information, "提示");
																		}
																		else
																		{
																			Interaction.MsgBox("当前选中校验方式为系统内置函数不能修改！", MsgBoxStyle.Information, "提示");
																		}
																	}
																	
																	public void LB_CodeList_DoubleClick(object sender, System.EventArgs e)
																	{
																		try
																		{
																			string SelectedString = System.Convert.ToString(LB_CodeList.Items[LB_CodeList.SelectedIndex]);
																			CommFunction.CheckMode selectedMode = null;
																			if (string.IsNullOrEmpty(SelectedString) == false)
																			{
																				for (var i = 0; i <= CommFunction.CheckData.Count - 1; i++)
																				{
																					if (SelectedString.ToUpper() == CommFunction.CheckData[System.Convert.ToInt32(i)].CheckName.ToUpper)
																					{
																						selectedMode = CommFunction.CheckData[System.Convert.ToInt32(i)];
																						break;
																					}
																				}
																			}
																			try
																			{
																				TB_CheckName.Text = selectedMode.CheckName;
																				TB_CheckFunName.Text = selectedMode.CheckFunName;
																				CheckDescription.Text = selectedMode.CheckDescription;
																				if (selectedMode.CodeMode != CommFunction.EnumCodeMode.None)
																				{
																					CBCodeMode.SelectedIndex = (System.Int32) selectedMode.CodeMode;
																				}
																				else
																				{
																					CBCodeMode.SelectedIndex = 0;
																					CheckDescription.Text = "内置函数";
																				}
																				CheckCode_Script.Text = selectedMode.CheckCode;
																				CheckLength.Text = System.Convert.ToString(selectedMode.CheckLength);
																				//  If Not (String.IsNullOrEmpty(.CheckCode)) Then CheckCode_Script.FreshString()
																			}
																			catch (Exception ex)
																			{
																				Interaction.MsgBox(ex.Message, (int) MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "错误");
																			}
																		}
																		catch (Exception ex)
																		{
																			Interaction.MsgBox(ex.Message, (int) MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "错误");
																		}
																	}
																	
																	public void LB_CodeList_SelectedIndexChanged(System.Object sender, System.EventArgs e)
																	{
																		
																	}
																	
																	public void Btn_CheckDel_Click(System.Object sender, System.EventArgs e)
																	{
																		string tmp = TB_CheckName.Text;
																		string sc = TB_CheckFunName.Text;
																		int ObjIndex = -1;
																		CommFunction.CheckMode ModyfyCheck;
																		if (CommFunction.SaveData.CheckNumberMode != null)
																		{
																			for (var i = 0; i <= CommFunction.SaveData.CheckNumberMode.Count - 1; i++)
																			{
																				if (CommFunction.SaveData.CheckNumberMode[System.Convert.ToInt32(i)].CheckName.ToUpper == tmp.ToUpper() && CommFunction.SaveData.CheckNumberMode[System.Convert.ToInt32(i)].CheckFunName.ToUpper == sc.ToUpper())
																				{
																					ObjIndex = System.Convert.ToInt32(i);
																					ModyfyCheck = CommFunction.SaveData.CheckNumberMode[System.Convert.ToInt32(i)];
																				}
																			}
																		}
																		if (ObjIndex >= 0)
																		{
																			if (Interaction.MsgBox("是否删除当前选中的校验方式【" + tmp + "】？", (int) MsgBoxStyle.Question + MsgBoxStyle.YesNo, "询问") == MsgBoxResult.Yes)
																			{
																				CommFunction.SaveData.CheckNumberMode.RemoveAt(ObjIndex);
																				for (var i = LB_CodeList.Items.Count - 1; i >= 0; i--)
																				{
																					if (LB_CodeList.Items[System.Convert.ToInt32(i)].ToString().ToUpper() == tmp.ToUpper())
																					{
																						LB_CodeList.Items.RemoveAt(System.Convert.ToInt32(i));
																					}
																				}
																				Form1.Default.SaveSetting();
																				ClearAllText(TB_CheckFunName.Parent);
																				Interaction.MsgBox("数据删除成功！", MsgBoxStyle.Information, "提示");
																			}
																		}
																		else
																		{
																			Interaction.MsgBox("该校验方式为系统内置函数不能删除！", MsgBoxStyle.Information, "提示");
																		}
																	}
																	private void ClearAllText(Control Parent)
																	{
																		Type tp;
																		foreach (object item in Parent.Controls)
																		{
																			tp = item.GetType();
																			if (tp.ToString().ToUpper() == "System.Windows.Forms.TextBox".ToUpper())
																			{
																				item.text = "";
																			}
																		}
																	}
																	public void Btn_DataBaseAdd_Click(System.Object sender, System.EventArgs e)
																	{
																		string TableName = TB_SearchTable.Text;
																		if (string.IsNullOrEmpty(TB_SearchTable.Text))
																		{
																			Interaction.MsgBox("搜索数据的表明不能为空！", MsgBoxStyle.Critical, "错误");
																		}
																		return ;
//																		if (string.IsNullOrEmpty(TB_LoadDataSql.Text))
//																		{
//																			Interaction.MsgBox("加载数据库的Sql语句不能为空！", MsgBoxStyle.Critical, "错误");
//																			}
//																			return ;
//																			if (string.IsNullOrEmpty(TB_SearchSql.Text))
//																			{
//																				Interaction.MsgBox("搜索数据库的Sql语句不能为空！", MsgBoxStyle.Critical, "错误");
//																				}
//																				return ;
//																				if (string.IsNullOrEmpty(TB_LoadDataVBS.Text))
//																				{
//																					Interaction.MsgBox("加载数据到生产数据显示表的VBS脚本代码不能为空，在添加前最好先测试代码！", MsgBoxStyle.Critical, "错误");
//																					}
//																					return ;
//																					if (CommFunction.SaveData.SearchTable != null)
//																					{
//																						for (var i = 0; i <= CommFunction.SaveData.SearchTable.Count - 1; i++)
//																						{
//																							if (CommFunction.SaveData.SearchTable[System.Convert.ToInt32(i)].TableName.ToUpper == TableName.ToUpper() && CommFunction.SaveData.DataBase.ToUpper() == CommFunction.SaveData.SearchTable[System.Convert.ToInt32(i)].DataBaseName.ToUpper)
//																							{
//																								Interaction.MsgBox("要搜索的数据库表已经存在，请选择其他表！", (int) MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示");
//																								return ;
//																								}
//																								}
//																								}
//																								CommFunction.SearchData tmpData = new CommFunction.SearchData();
//																								tmpData.DataBaseName = CommFunction.SaveData.DataBase;
//																								tmpData.TableName = TableName;
//																								tmpData.SearchSql = TB_SearchSql.Text;
//																								tmpData.LoadDataVBS = TB_LoadDataVBS.Text;
//																								tmpData.DownLoadDataSql = TB_LoadDataSql.Text;
//																								tmpData.DescriptionSql = TB_DescriptionSql.Text;
//																								if (CommFunction.SaveData.SearchTable == null)
//																								{
//																									CommFunction.SaveData.SearchTable = new List<CommFunction.SearchData>();
//																									}
//																									CommFunction.SaveData.SearchTable.Add(tmpData);
//																									LB_SearchTable.Items.Add(tmpData.TableName);
//																									Form1.Default.SaveSetting();
//																									Interaction.MsgBox("数据保存成功！", MsgBoxStyle.Information, "提示");
																								}
																								
																								public void Btn_DataBaseRun_Click(System.Object sender, System.EventArgs e)
																								{
																									// ClearAllText(Btn_DataBaseRun.Parent)
																									
																									try
																									{
																										RunVBScript.AddCode(TB_LoadDataVBS.Text);
																										RunVBScript.Run(CommFunction.getVbsSubOrFunName(TB_LoadDataVBS.Text));
																									}
																									catch (Exception ex)
																									{
																										MessageBox.Show(ex.Message);
																									}
																								}
																								
																								public void LB_SearchTable_DoubleClick(object sender, System.EventArgs e)
																								{
																									string tmp = "";
																									CommFunction.SearchData tmpData = new CommFunction.SearchData();
																									try
																									{
																										tmp = LB_SearchTable.Items[LB_SearchTable.SelectedIndex].ToString();
																										if (CommFunction.SaveData.SearchTable != null)
																										{
																											for (var i = 0; i <= CommFunction.SaveData.SearchTable.Count - 1; i++)
																											{
																												if (CommFunction.SaveData.SearchTable[System.Convert.ToInt32(i)].TableName.ToUpper == tmp.ToUpper())
																												{
																													tmpData = CommFunction.SaveData.SearchTable[System.Convert.ToInt32(i)];
																												}
																											}
																										}
																										
																										// .DataBaseName = SaveData.DataBase
																										TB_SearchTable.Text = tmpData.TableName;
																										TB_SearchSql.Text = tmpData.SearchSql;
																										TB_LoadDataVBS.Text = tmpData.LoadDataVBS;
																										TB_LoadDataSql.Text = tmpData.DownLoadDataSql;
																										TB_DescriptionSql.Text = tmpData.DescriptionSql;
																									}
																									catch (Exception ex)
																									{
																										Interaction.MsgBox(ex.Message, MsgBoxStyle.Critical, "错误");
																									}
																								}
																								
																								public void LB_SearchTable_SelectedIndexChanged(System.Object sender, System.EventArgs e)
																								{
																									
																								}
																								
																								public void TB_SearchTable_TextChanged(System.Object sender, System.EventArgs e)
																								{
																									TB_DescriptionSql.Text = DescriptionSql1 + TB_SearchTable.Text + DescriptionSql2;
																									if (string.IsNullOrEmpty(TB_LoadDataVBS.Text))
																									{
																										TB_LoadDataVBS.AppendText("Function " + TB_SearchTable.Text + "(Param,TB_Prefix,TB_Suffix)");
																										TB_LoadDataVBS.AppendText("\r\n" + "\r\n" + "End Function");
																									}
																									else
																									{
																										TB_LoadDataVBS.Text = ChangeFunName(TB_LoadDataVBS.Text, TB_SearchTable.Text);
																									}
																								}
																								
																								public void Btn_SearchData_Click(System.Object sender, System.EventArgs e)
																								{
																									string tmpName = TB_SearchTable.Text;
																									CommFunction.SearchData TmpData = new CommFunction.SearchData();
																									if (CommFunction.SaveData.SearchTable != null)
																									{
																										for (var i = 0; i <= CommFunction.SaveData.SearchTable.Count - 1; i++)
																										{
																											if (tmpName.ToUpper() == CommFunction.SaveData.SearchTable[System.Convert.ToInt32(i)].TableName.ToUpper)
																											{
																												if (Interaction.MsgBox("是否修改该表搜索下载数据的设置？", (int) MsgBoxStyle.Question + MsgBoxStyle.OkCancel, "询问") == MsgBoxResult.Ok)
																												{
																													TmpData.TableName = TB_SearchTable.Text;
																													TmpData.SearchSql = TB_SearchSql.Text;
																													TmpData.LoadDataVBS = TB_LoadDataVBS.Text;
																													TmpData.DownLoadDataSql = TB_LoadDataSql.Text;
																													TmpData.DescriptionSql = TB_DescriptionSql.Text;
																													TmpData.DataBaseName = CommFunction.SaveData.DataBase;
																													CommFunction.SaveData.SearchTable[System.Convert.ToInt32(i)] = TmpData;
																													Form1.Default.SaveSetting();
																													Interaction.MsgBox("修改数据保存成功！", MsgBoxStyle.Information, "提示");
																												}
																											}
																										}
																									}
																								}
																								
																								private void TB_LoadDataVBS_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
																								{
																									ToolTip1.Show("  1.必须是完整的函数过程；" + "\r\n" + "  2.函数有一个参数，参数类型为ArrayList；" + "\r\n" + "  3.函数返回值为字符型数据，并且以 | 符号分隔对应到数据表的每列数据；" + "\r\n" + "  4.返回值中数据段必须等于7。", this);
																								}
																								private string ChangeFunName(string Code, string Name)
																								{
																									string[] fun = null;
																									string Value = "";
																									bool Fstart = false;
																									string Parame = "";
																									bool EndCode = false;
																									bool NR = false;
																									fun = Code.Split('\n');
																									for (var i = 0; i <= fun.Length - 1; i++)
																									{
																										//  If fun(i).Trim <> "" Then
																										if ((fun[(int) i].ToUpper()).IndexOf("Function ".ToUpper()) + 1 > 0)
																										{
																											Fstart = true;
																											int a = System.Convert.ToInt32(fun[(int) i].IndexOf("("));
																											int b = System.Convert.ToInt32(fun[(int) i].IndexOf(")"));
																											if (b - a > 1)
																											{
																												Parame = System.Convert.ToString(fun[(int) i].Substring(a + 1, b - a - 1));
																												string[] n = Parame.Split(',');
																												if (n.Length != 3)
																												{
																													Parame = "Param,TB_Prefix,TB_Suffix";
																												}
																											}
																											else
																											{
																												Parame = "Param,TB_Prefix,TB_Suffix";
																											}
																											if (string.IsNullOrEmpty(Value))
																											{
																												Value = "Function " + Name + "(" + Parame + ")";
																											}
																											else
																											{
																												Value += "\r\n" + "Function " + Name + "(" + Parame + ")";
																											}
																										}
																										else if (fun[(int) i].ToUpper().IndexOf("End ".ToUpper()) < 0 && fun[(int) i].ToUpper().IndexOf(" Function".ToUpper()) < 0)
																										{
																											if (string.IsNullOrEmpty(Value))
																											{
																												Value = fun[(int) i];
																											}
																											else
																											{
																												Value += "\r\n" + fun[(int) i];
																											}
																											if (fun[(int) i].Trim() != "")
																											{
																												NR = true;
																											}
																											EndCode = false;
																										}
																										else if (fun[(int) i].ToUpper().IndexOf("End ".ToUpper()) >= 0 && fun[(int) i].ToUpper().IndexOf(" Function".ToUpper()) > 0)
																										{
																											if (NR == true)
																											{
																												Value += "\r\n" + fun[(int) i];
																											}
																											else
																											{
																												Value += "\r\n" + "\r\n" + "\'在此处添加函数内容" + "\r\n" + fun[(int) i];
																											}
																											EndCode = true;
																										}
																									}
																									if (EndCode == false)
																									{
																										if (NR == false)
																										{
																											Value += "\r\n" + "\r\n" + "\'在此处添加函数内容" + "\r\n" + "End Function";
																										}
																									}
																									return Value;
																								}
																								
																								public void Btn_ModyfyFormat_Click(System.Object sender, System.EventArgs e)
																								{
																									string tmp = "";
																									if (SaveDataFormat.SelectedIndex < 0)
																									{
																										Interaction.MsgBox("请先选中要修改保存名称的数据！", (int) MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "提示");
																										return ;
																									}
																									else
																									{
																										tmp = System.Convert.ToString(SaveDataFormat.Items[SaveDataFormat.SelectedIndex]);
																										int SC = CommFunction.SaveData.Setting.Count - 1;
																										int FindIndex = -1;
																										CommFunction.SaveSetting FindData = null;
																										bool HasFind = false;
																										for (var i = 0; i <= SC; i++)
																										{
																											if (tmp.ToUpper() == CommFunction.SaveData.Setting[i].SaveName.ToUpper)
																											{
																												FindIndex = System.Convert.ToInt32(i);
																												FindData = CommFunction.SaveData.Setting[i];
																												break;
																											}
																										}
																										if (FindIndex >= 0)
																										{
																											string ModifyName = RemedyCode.ShowInput(this, "请输入修改内容", FindData.SaveName, false);
																											bool HasExist = false;
																											if (string.IsNullOrEmpty(ModifyName))
																											{
																												return ;
																											}
																											if (ModifyName != "")
																											{
																												for (var i = 0; i <= SC; i++)
																												{
																													if (ModifyName.ToUpper() == CommFunction.SaveData.Setting[i].SaveName.ToUpper)
																													{
																														HasExist = true;
																														break;
																													}
																												}
																											}
																											if (HasExist)
																											{
																												Interaction.MsgBox("输入的内容已经存在，请重新输入！", MsgBoxStyle.Information, "提示");
																											}
																											else
																											{
																												FindData.SaveName = ModifyName;
																												CommFunction.SaveData.Setting[FindIndex] = FindData;
																												SaveDataFormat.Items[SaveDataFormat.SelectedIndex] = FindData.SaveName;
																												Form1.Default.SaveSetting();
																												Form1.Default.ModifySetting(tmp, FindData.SaveName);
																												Interaction.MsgBox("数据修改成功！", MsgBoxStyle.Information, "提示");
																											}
																										}
																									}
																								}
																								
																								public void Btn_DeleteTable_Click(System.Object sender, System.EventArgs e)
																								{
																									string tmp = "";
																									try
																									{
																										tmp = System.Convert.ToString(LB_SearchTable.Items[LB_SearchTable.SelectedIndex]);
																									}
																									catch
																									{
																									}
																									if (LB_SearchTable.SelectedIndex < 0)
																									{
																										Interaction.MsgBox("请先选择要删除的数据表！", MsgBoxStyle.Information, "提示");
																									}
																									return ;
//																									for (var i = 0; i <= CommFunction.SaveData.SearchTable.Count - 1; i++)
//																									{
//																										if (tmp.ToUpper() == CommFunction.SaveData.SearchTable[System.Convert.ToInt32(i)].TableName.ToUpper)
//																										{
//																											LB_SearchTable.Items.Remove(tmp);
//																											CommFunction.SaveData.SearchTable.RemoveAt(System.Convert.ToInt32(i));
//																											Interaction.MsgBox("数据表已从本地删除成功！", MsgBoxStyle.OkOnly, "提示");
//																											return ;
//																											}
//																											}
																										}
																										
																										public void TabPage2_Resize(object sender, EventArgs e)
																										{
																											SaveDataFormat.Width = (int) ((double) (TabPage2.Width - 85) / 2);
																											RemovedFormat.Width = (int) ((double) (TabPage2.Width - 85) / 2 - 5);
																											RemovedFormat.Left = TabPage2.Width - RemovedFormat.Width;
																											SaveDataFormat.Height = TabPage2.Height - SaveDataFormat.Top;
																											RemovedFormat.Height = TabPage2.Height - RemovedFormat.Top;
																											Label2.Left = RemovedFormat.Left;
																											Btn_Remove.Left = SaveDataFormat.Width + SaveDataFormat.Left + 5;
																											Btn_ModyfyFormat.Left = Btn_Remove.Left;
																											Btn_Recoevry.Left = Btn_Remove.Left;
																											Btn_FormatSave.Left = Btn_Remove.Left;
																										}
																										
																										public void CBCodeMode_SelectedIndexChanged(object sender, EventArgs e)
																										{
																											CheckCode_Script.ClearStylesBuffer();
																											CheckCode_Script.Range.ClearStyle(StyleIndex.All);
																											InitStylesPriority();
																											if (CBCodeMode.SelectedIndex == 0)
																											{
																												CheckCode_Script.Language = FastColoredTextBoxNS.Language.VB;
																											}
																											else
																											{
																												CheckCode_Script.Language = FastColoredTextBoxNS.Language.CSharp;
																											}
																											
																										}
																										private void InitStylesPriority()
																										{
																											CheckCode_Script.ClearStylesBuffer();
																											//add this style explicitly for drawing under other styles
																											CheckCode_Script.AddStyle(SameWordsStyle);
																										}
																										public void CBCodeMode_SelectedValueChanged(object sender, EventArgs e)
																										{
																											if (CBCodeMode.SelectedIndex == 0)
																											{
																												CheckCode_Script.Language = FastColoredTextBoxNS.Language.VB;
																											}
																											else
																											{
																												CheckCode_Script.Language = FastColoredTextBoxNS.Language.JS;
																											}
																										}
																										
																										public void SystemSet_Resize(object sender, EventArgs e)
																										{
																											
																										}
																										
																										public void CheckCode_Script_TextChanged(object sender, TextChangedEventArgs e)
																										{
																											if (CBCodeMode.SelectedIndex > 0)
																											{
																												CSharpSyntaxHighlight(e);
																											}
																										}
																										private void CSharpSyntaxHighlight(TextChangedEventArgs e)
																										{
																											
																											CheckCode_Script.LeftBracket = '(';
																											CheckCode_Script.RightBracket = ')';
																											CheckCode_Script.LeftBracket2 = "\\x0";
																											CheckCode_Script.RightBracket2 = "\\x0";
																											//clear style of changed range
																											e.ChangedRange.ClearStyle(BlueStyle, BoldStyle, GrayStyle, MagentaStyle, GreenStyle, BrownStyle);
																											
																											//string highlighting
																											e.ChangedRange.SetStyle(BrownStyle, "\"\"|@\"\"|\'\'|@\".*?\"|(?<!@)(?<range>\".*?[^\\\\]\")|\'.*?[^\\\\]\'");
																											//comment highlighting
																											e.ChangedRange.SetStyle(GreenStyle, "//.*$", RegexOptions.Multiline);
																											e.ChangedRange.SetStyle(GreenStyle, "(/\\*.*?\\*/)|(/\\*.*)", RegexOptions.Singleline);
																											e.ChangedRange.SetStyle(GreenStyle, "(/\\*.*?\\*/)|(.*\\*/)", RegexOptions.Singleline | RegexOptions.RightToLeft);
																											//number highlighting
																											e.ChangedRange.SetStyle(MagentaStyle, "\\b\\d+[\\.]?\\d*([eE]\\-?\\d+)?[lLdDfF]?\\b|\\b0x[a-fA-F\\d]+\\b");
																											//attribute highlighting
																											e.ChangedRange.SetStyle(GrayStyle, "^\\s*(?<range>\\[.+?\\])\\s*$", RegexOptions.Multiline);
																											//class name highlighting
																											e.ChangedRange.SetStyle(BoldStyle, "\\b(class|struct|enum|interface)\\s+(?<range>\\w+?)\\b");
																											//keyword highlighting
																											e.ChangedRange.SetStyle(BlueStyle, "\\b(abstract|function|as|base|bool|break|byte|case|catch|char|checked|class|const|continue|decimal|default|delegate|do|double|else|enum|event|explicit|extern|false|finally|fixed|float|for|foreach|goto|if|implicit|in|int|interface|internal|is|lock|long|namespace|new|null|object|operator|out|override|params|private|protected|public|readonly|ref|return|sbyte|sealed|short|sizeof|stackalloc|static|string|struct|switch|this|throw|true|try|typeof|uint|ulong|unchecked|unsafe|ushort|using|virtual|void|volatile|while|add|alias|ascending|descending|dynamic|from|get|global|group|into|join|let|orderby|partial|remove|select|set|value|var|where|yield)\\b|#region\\b|#endregion\\b");
																											
																											//clear folding markers
																											e.ChangedRange.ClearFoldingMarkers();
																											//set folding markers
																											e.ChangedRange.SetFoldingMarkers("{", "}"); //allow to collapse brackets block
																											e.ChangedRange.SetFoldingMarkers("#region\\b", "#endregion\\b"); //allow to collapse #region blocks
																											e.ChangedRange.SetFoldingMarkers("/\\*", "\\*/"); //allow to collapse comment block
																										}
																										
																									}
																								}
